<template>
  <div class="personal-page">
    <Layout>
      <!-- 头部区域 -->
      <Header>
        <div class="left">
          <logo></logo>
          <!-- 导入 -->
          <Divider type="vertical" />
          在线设计工具
        </div>

        <div class="right">
<!--          <Button type="primary" to="/edit" size="smail" target="_blank">新建设计</Button>-->
          <Button type="primary" size="smail" @click="addProject">新建项目</Button>
          <Divider type="vertical" />
          <!-- 预览 -->
          <login></login>
          <!--          <lang></lang>-->
        </div>
      </Header>

      <Content>
        <div class="tool">
          <div class="tool-left">
            <Input prefix="ios-search" v-model="searchValue" placeholder="搜索项目" style="width: 400px" />
            <Button type="primary" @click="search">搜 索</Button>
            <Button @click="reset">重 置</Button>
          </div>
          <div class="tool-right"></div>
        </div>
        <div class="list">
          <div class="list-content" v-if="list.length > 0">
            <div class="list-item" v-for="(item,index) in list" :key="index">
              <div class="list-item-top">
                <span>{{ item.title }}</span>
              </div>
              <div class="list-item-bottom">
                <div class="list-item-bottom-left" @click="goWork(item)">
                  <div class="icon">
                    <img src="@/assets/image/list.png" style="width: 100%;height: 100%;">
                  </div>
                  <div class="edit">
                    <span>工作台</span>
<!--                    <Button type="text" :to="'/edit/'+ item.id" target="_blank">工作台</Button>-->
                  </div>
                </div>
                <div class="list-item-bottom-right" @click="deleteItem(item)">
                  <div class="icon">
                    <img src="@/assets/image/delete.png" style="width: 100%;height: 100%;">
                  </div>
                  <div class="edit">
                    <span>删除</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="empty" v-else>
            <div class="empty-img">
              <img src="@/assets/image/empty.png" style="width: 100%;height: 100%;">
            </div>
            <div class="empty-text">
              <span>暂无项目</span>
            </div>
          </div>
          <!-- 新建项目对话框 -->
          <div>
            <Modal v-model="addModal" :title="addTitle" footer-hide>
              <div>
                <Form ref="addFormRef" :model="addFrom" :rules="addRule" :label-width="80">
                  <FormItem label="项目名称" prop="title">
                    <Input v-model="addFrom.title" placeholder="请输入项目名称"></Input>
                  </FormItem>
                  <FormItem>
                    <div style="width: 100%;display: flex;justify-content: flex-end;gap: 12px;">
                      <Button long @click="cancelAdd" style="width: 80px;">取 消</Button>
                      <Button type="primary" long @click="asyncOKAdd" style="width: 80px;">确 定</Button>
                    </div>
                  </FormItem>
                </Form>
              </div>
            </Modal>
          </div>
          <!-- 删除对话框 -->
          <div>
            <Modal v-model="deleteModal" :title="deleteTitle" :loading="deleteLoading" @on-ok="asyncOKDelete">
              <div>
                {{ deleteFrom.title }}
              </div>
            </Modal>
          </div>
        </div>
      </Content>

      <Footer class="layout-footer-center">
        2024 &copy; 鹿卡设计-图片编辑器
      </Footer>
    </Layout>
  </div>
</template>

<script name="personal" setup>
import { useRouter } from 'vue-router';
const router = useRouter();
// 顶部组件
import logo from '@/components/logo.vue';
import lang from '@/components/lang.vue';
import login from '@/components/login.vue';

import {
  getAddProject,
  getDelete,
  getWorkList
} from "@/api/personal";
import { setToken, setUserName } from "@/api/user";
import { Message } from "view-ui-plus";

const list = ref([]);
const total  = ref(0)
const searchValue = ref('');

const deleteModal = ref(false);
const deleteTitle = ref('删除');
const deleteLoading = ref(false);
const deleteFrom = ref({});

const addModal = ref(false);
const addTitle = ref('新建项目');
const addLoading = ref(false);
const addFrom = ref({
  title: ''
});
const addRule = {
  title: [
    { required: true, message: '请输入项目名称', trigger: 'blur' }
  ],
}

const queryFrom = ref({
  pageNum:1,
  pageSize:10
})

/**
 * 获取列表
 * */
const getList = ()=>{
  getWorkList(queryFrom.value).then((res)=>{
    console.log("res",res);
    let data = res.data.data
    list.value = data.rows
    console.log("list",list.value);
    total.value = data.total
    console.log("total",total.value);
  }).catch(()=>{

  })
}

/**
 * 删除
 * */
const deleteItem = (item)=>{
  deleteModal.value = true
  deleteFrom.value = Object.assign({},item)
  deleteLoading.value = true
}

/**
 * 搜索
 * */
function search(){
  console.log("searchValue",searchValue.value);
  queryFrom.value.title = searchValue.value;
  getList();
}

function reset(){
  searchValue.value = '';
  queryFrom.value = {};
  queryFrom.value.pageNum = 1
  queryFrom.value.pageSize = 10
  getList();
}

const asyncOKDelete = () => {
  getDelete({id:deleteFrom.value.id}).then((res)=>{
    const code = res.data.code;
    if(code === 200){
      Message.success('删除成功！');
      deleteLoading.value = false
      deleteModal.value = false
      getList();
    }else{
      Message.error('删除失败！');
    }
  })
}

/**
 * 工作台
 * */
const goWork = (item) => {
  console.log("item",item);

  // 解析目标位置
  const route = router.resolve({
    name: 'edit',
    params: { id: item.id } // 使用params代替query
  });
  // 在新的浏览器标签中打开链接
  window.open(route.href, '_blank');
}

/**
 * 新建项目
 * */
const addProject = ()=>{
  addModal.value = true;
  addFrom.value = {}
}

const addFormRef = ref(null);
const asyncOKAdd = ()=>{
  addFormRef.value.validate((valid) => {
    if (!valid) return;
    getAddProject({title: addFrom.value.title}).then((res)=>{
      const code = res.data.code;
      if(code === 200){
        Message.success('新增成功！');
        addModal.value = false;
        addFrom.value = {}
        getList();
      }else{
        Message.error('新增失败！');
      }
    })
  })
}

/**
 * 取消
 * */
const cancelAdd = ()=>{
  addModal.value = false;
  addFrom.value = {}
}

getList();
</script>

<style lang="less" scoped>
.personal-page{
  width: 100%;
  height: 100vh;
  box-sizing: border-box;
  overflow: hidden;
  background-color: #f5f7fa;
  display: flex;
  flex-direction: column;
}

// header
:deep(.ivu-layout-header) {
  width: 100%;
  --height: 45px;
  padding: 0 0;
  border-bottom: 1px solid #eef2f8;
  background: #fff;
  height: var(--height);
  line-height: var(--height);
  display: flex;
  justify-content: space-between;

  .left, .right {
    display: flex;
    align-items: center;
    img {
      display: block;
      margin-right: 10px;
    }
  }
}

// content
:deep(.ivu-layout-content) {
  width: 100%;
  --height: calc(100% - 45px - 69px);
  padding: 0 0;
  height: var(--height);
  display: flex;
  flex-direction: column;
}

// tool
.tool{
  width: 100%;
  height: 70px;
  padding: 0 20px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;

  .tool-left{
    flex: 1;
    height: 70px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 12px;
  }
  .tool-right {
    flex: 1;
    height: 70px;
  }
}

// list
.list {
  width: 100%;
  flex: 1;
  padding: 20px 0 20px;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  overflow-y: auto;

  .list-content{
    width: 1200px;
    height: 10px;
    box-sizing: border-box;
    display: grid;
    grid-template-columns: repeat(4, 1fr); /* 创建四列 */
    gap: 30px; /* 设置项目之间的间距 */
    align-items: flex-start; /* 确保子项顶部对齐 */

    .list-item {
      background-color: rgba(255, 255, 255, 0.8);
      border: 1px solid rgba(0, 0, 0, 0.4);
      height: 90px;
      box-sizing: border-box;
      border-radius: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      overflow: hidden;

      .list-item-top{
        width: 100%;
        flex: 1;
        box-sizing: border-box;
        padding: 10px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        border-bottom: 1px solid #dcdee2;
      }
      .list-item-bottom{
        width: 100%;
        height: 30px;
        display: flex;
        justify-content: center;
        align-items: center;

        .list-item-bottom-left,.list-item-bottom-right{
          flex: 1;
          height: 40px;
          display: flex;
          justify-content: center;
          align-items: center;
          gap: 10px;
          cursor: pointer;
        }

        .list-item-bottom-right{
          border-left: 1px solid #dcdee2;
        }

        .icon{
          width: 18px;
          height: 18px;
        }
        .edit{
          font-size:14px;
        }
      }
    }
  }

  .empty{
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    gap: 50px;

    .empty-img{
      width: 256px;
      height: auto;
    }
    .empty-text{
      font-size: 24px;
      font-weight: 500;
      color: #333333;
    }
  }
}

// footer
:deep(.ivu-layout-footer) {
  width: 100%;
  --height: 69px;
  padding: 0 0;
  border-top: 1px solid #eef2f8;
  background: #fff;
  height: var(--height);
  line-height: var(--height);
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
